﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Rose.Controllers
{
    [Authorize(Roles="Admin")]
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/
        
        public ActionResult Index()
        {
            ViewData["PageTitle"] = "Index";
            List<string> lovers = new List<string>();
            if (Roles.RoleExists("Lover"))
            {
                foreach (string lover in Roles.GetUsersInRole("Lover"))
                {
                    lovers.Add(lover);
                }
            }
            ViewData["LoverCount"] = lovers.Count();
            ///情路数量
            ViewData["OnlineCount"] = Membership.GetNumberOfUsersOnline();
            ViewData["RegisterCount"] = Membership.GetAllUsers().Count;
            return View();
            
        }
        public ActionResult ManageRoles()
        {
            List<string> roles = new List<string>();
            List<string> admins = new List<string>();
            List<string> editors = new List<string>();
            foreach (string role in Roles.GetAllRoles())
            {
                roles.Add(role);
            }
            if (Roles.RoleExists("Admin"))
            {
                foreach (string user in Roles.GetUsersInRole("Admin"))
                {
                    admins.Add(user);
                }

            }

            if (Roles.RoleExists("Editor"))
            {
                foreach (string user in Roles.GetUsersInRole("Editor"))
                {
                    editors.Add(user);
                }
            }
            ViewData["PageTitle"] = "Manage Roles";
            
            ViewData["AllRoles"] = roles;
            ViewData["AllAdmins"] = admins;
            ViewData["AllEditors"] = editors;

           

            return View();

        }

        [HttpPost]
        public ActionResult AddRole(string newrole)
        {
            if (!Roles.RoleExists(newrole))
            {
                Roles.CreateRole(newrole);
            }
            return RedirectToAction("ManageRoles");
        }

        [HttpPost]
        public ActionResult AddUserToAdmin(string userId)
        {
            if (Roles.RoleExists("Admin"))
            {
                Roles.AddUserToRole(userId, "Admin");
                return RedirectToAction("ManageRoles");
            }
            else return Content("错误");
        }

        [HttpPost]
        public ActionResult AddUserToEditor(string userId)
        {
            if (Roles.RoleExists("Editor"))
            {
                Roles.AddUserToRole(userId, "Editor");
                return RedirectToAction("ManageRoles");
            }
            else return Content("错误");
        }

        [HttpPost]
        public ActionResult RemoveUserFromAdmin(string userId)
        {
            if (Roles.RoleExists("Admin")&&Roles.IsUserInRole(userId,"Admin"))
            {
                Roles.RemoveUserFromRole(userId, "Admin");
                return RedirectToAction("ManageRoles");
            }
            else return Content("错误");
        }

        [HttpPost]
        public ActionResult RemoveUserFromEditor(string userId)
        {
            if (Roles.RoleExists("Editor") && Roles.IsUserInRole(userId, "Editor"))
            {
                Roles.RemoveUserFromRole(userId, "Editor");
                return RedirectToAction("ManageRoles");
            }
            else return Content("错误");
        }


        public ActionResult ShowAllLovers()
        {
            ViewData["PageTitle"]="查看所有情侣注册用户";
            if (Roles.RoleExists("Lover"))
            {
                List<string> lovers = new List<string>();
                foreach (string lover in Roles.GetUsersInRole("Lover"))
                {
                    lovers.Add(lover);
                }

                ViewData["AllLovers"] = lovers;
                ViewData["LoverCount"] = lovers.Count();
                return View();
            }
            else return Content("错误");
        }
        
    }
}
